package cn.edu.neu.vmmigeval.cloud.policy.impl;


import cn.edu.neu.vmmigeval.cloud.entity.Host;
import cn.edu.neu.vmmigeval.cloud.entity.Vm;
import cn.edu.neu.vmmigeval.cloud.policy.MigratedHostPolicy;
import cn.edu.neu.vmmigeval.cloud.policy.VmSelectionPolicy;

import java.util.ArrayList;
import java.util.List;

public class VmSelectionPolicyDefault extends VmSelectionPolicy {
	
	private double threshold;
	
	public VmSelectionPolicyDefault(MigratedHostPolicy migratedHostPolicy, double threshold) {
		super(migratedHostPolicy);
		this.threshold = threshold;
	}
	
	public VmSelectionPolicyDefault(double threshold){
		this.threshold = threshold;
	}
	
	
	

	public List<Vm> getMigratedVmsfromHost1(Host host){
		
		List<Vm> vms = new ArrayList<Vm>();
		
		double cpuUtilization = 0 ;
		
		for (Vm vm : host.getVmList()) {
			cpuUtilization = (double)(host.getCurrentCpu()-vm.getCurrentCpu())/host.getTotalCpu();
			if(cpuUtilization<threshold){
				vms.add(vm);
				break;
			}else{
				vms.add(vm);
			}
		}
		return vms;
	}
	
	@Override
	public List<Vm> getMigratedVmsfromHost(Host host){
		
		List<Vm> vms = new ArrayList<Vm>();
		
		double cpuUtilization = 0 ;
		
		for (Vm vm : host.getVmList()) {
			int totalCpus = 0;
			for (Vm vm2 : vms) {
				totalCpus+=vm2.getCurrentCpu();
			}
			cpuUtilization = (double)(host.getCurrentCpu()-totalCpus)/host.getTotalCpu();
			if(cpuUtilization<threshold){
				vms.add(vm);
				break;
			}else{
				vms.add(vm);
			}
		}
		return vms;
	}

	public double getThreshold() {
		return threshold;
	}

	public void setThreshold(double threshold) {
		this.threshold = threshold;
	}

	@Override
	public Vm getVmToMigrate(Host host) {
		// TODO Auto-generated method stub
		return null;
	}
	
	

}
